(define-module (data-mining hash-util)
  #:export (hash-table-size
	    hash-move-key
	    hash-subset))

(define (hash-table-size table)
  "Return the number of association in the hash table."
  (hash-count (const #t) table))

(define* (hash-move-key table oldkey newkey #:optional (default #f))
  "Modify TABLE such that the value pointed to by OLDKEY is pointed to
by NEWKEY instead."
  (let ((val (hash-ref table oldkey default)))
    (hash-remove! table oldkey)
    (hash-set! table newkey val)))

(define (hash-subset table keys)
  "Return a new hash table which contains the keys KEYS from TABLE,
mapped to the same values."
  (let ((table* (make-hash-table)))
    (for-each
     (lambda (key)
       (hash-set! table* key
		  (hash-ref table key)))
     keys)
    table*))
